﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using ZLCMS.Data.Model;
using System.Web.UI.WebControls;
using NHibernate;

using NHibernate.Criterion;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.Interface.weixin.Html
{
    public partial class WxApplyDoc : UrlRewritePage
    {
        protected string _userAccount;//用户
        protected string _applyId;//预约配置ID
        protected WxApplyConfig _applyDb;
        protected string StatusMessage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = Request["account"];
            _applyId = Request["uid"];
            ViewState["openid"] = Request["openid"];
            _applyDb = GetApplyDb();
            StatusMessage = "";
        }

        #region 货物预约配置对象 -protected WxApplyConfig GetApplyDb()
        /// <summary>
        /// 货物预约配置对象
        /// </summary>
        /// <returns></returns>
        protected WxApplyConfig GetApplyDb()
        {
            WxApplyConfig _applyConfig;
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                _applyConfig = session.CreateCriteria<WxApplyConfig>().Add(Expression.Eq("OwnerAccount", _userAccount)).Add(Expression.Eq("Id", int.Parse(_applyId))).UniqueResult<WxApplyConfig>();
                if (_applyConfig == null)
                {
                    throw new Exception("找不到对应信息.");
                }
                return _applyConfig;
            }
        } 
        #endregion

        #region 提交预约 -protected void btnSubmit_Click(object sender, EventArgs e)
        /// <summary>
        /// 提交预约
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonSignup_Click(object sender, EventArgs e)
        {
            if ((string)Session["apply_status"] == "1")
            {
                StatusMessage = "不能重复预约";
            }
            else using (ISession session = SessionFactoryHelper.CreateSession())
            {
                WxApplyLog _applyLogDb = new WxApplyLog();

                _applyLogDb.ApplyItem1 = txtApplyItem1.Value;
                _applyLogDb.ApplyItem2 = txtApplyItem2.Value;
                _applyLogDb.ApplyItem3 = txtApplyItem3.Value;
                _applyLogDb.ApplyItem4 = txtApplyItem4.Value;
                _applyLogDb.ApplyItem5 = txtApplyItem5.Value;
                _applyLogDb.OptDate = DateTime.Now;
                _applyLogDb.WxId = ViewState["openid"] as string;
                _applyLogDb.OwnerAccount = _applyDb.OwnerAccount;
                _applyLogDb.ApplyId = Int32.Parse(_applyId);
                session.SaveOrUpdate(_applyLogDb);

                session.Flush();
                StatusMessage = "预约成功";
                Session["apply_status"] = "1";
            }
        } 
        #endregion


    }
}